package com.se.messageBox;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.util.Objects;

public class InformationAlert {
    private final String content;

    public InformationAlert(String str) {
        content = str;
    }

    public void show() {
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setResizable(false);
        stage.setTitle("提示信息");

        ImageView icon = new ImageView(new Image(Objects.requireNonNull(getClass().getResource("/image/alert.png")).toExternalForm()));
        icon.setFitWidth(32);
        icon.setFitHeight(32);

        Label contentLabel = new Label(content);
        contentLabel.setWrapText(true);
        contentLabel.setStyle("-fx-font-size: 14px; -fx-text-fill: #1a1a1a;");
        contentLabel.setMinWidth(250);

        Button okButton = getButton(stage);

        HBox contentLayout = new HBox(15);
        contentLayout.setAlignment(Pos.CENTER_LEFT);
        contentLayout.setPadding(new Insets(20));
        contentLayout.getChildren().addAll(icon, contentLabel);

        ButtonBar buttonBar = new ButtonBar();
        buttonBar.setPadding(new Insets(10, 20, 15, 20));
        ButtonBar.setButtonData(okButton, ButtonBar.ButtonData.OK_DONE);
        buttonBar.getButtons().add(okButton);

        VBox mainLayout = new VBox();
        mainLayout.setBackground(Background.fill(Color.WHITE));
        mainLayout.setStyle("-fx-border-color: #e5e5e5; -fx-border-width: 1;");
        mainLayout.getChildren().addAll(contentLayout, buttonBar);

        HBox.setHgrow(contentLabel, Priority.ALWAYS);
        contentLayout.setMaxWidth(Double.MAX_VALUE);
        HBox.setHgrow(contentLayout, Priority.ALWAYS);

        mainLayout.setFillWidth(true);
        mainLayout.setMaxWidth(Double.MAX_VALUE);

        Scene scene = new Scene(mainLayout);
        scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/com/se/css/alert.css")).toExternalForm());

        stage.setScene(scene);
        stage.setMinWidth(250);
        stage.setMinHeight(150);
        stage.sizeToScene();
        stage.showAndWait();
    }

    private static Button getButton(Stage stage) {
        Button okButton = new Button("确定");
        okButton.setStyle("-fx-background-color: #0078d4; -fx-text-fill: white; -fx-font-size: 14px;"
                + "-fx-padding: 5 20; -fx-background-radius: 4;");
        okButton.setOnAction(e -> stage.close());

        okButton.setOnMouseEntered(e -> okButton.setStyle("-fx-background-color: #106ebe; -fx-text-fill: white;"));
        okButton.setOnMouseExited(e -> okButton.setStyle("-fx-background-color: #0078d4; -fx-text-fill: white;"));
        return okButton;
    }
}
